package com.polaris.live.ui.user

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.KeyboardUtils
import com.polaris.live.R
import com.polaris.live.adapter.SearchUserAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.mvvm.base2.BaseVbFragment
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.util.DebounceHelper
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.FragmentSearchUserBinding
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.interface_bag.SearchUserListener
import com.polaris.live.net.liveService
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.utils.LiveUtils
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router

/**
 * SearchUserFragment
 *
 * @author Created by 半仙 on 2024/9/23/023 12:01
 */
class SearchUserFragment : BaseVbFragment<BaseViewModel, FragmentSearchUserBinding>(), SearchUserListener {

    private val mAdapter by lazy { SearchUserAdapter() }

    private val mType by lazy { arguments?.getString("type") ?: SearchUserActivity.USER }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.recycler.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(context)
        }
        initListener()
    }

    private fun initListener() {
        mAdapter.setOnItemChildClickListener { _, view, position ->
            DebounceHelper.singleClick {
                val item = mAdapter.getItemOrNull(position) ?: return@singleClick
                KeyboardUtils.hideSoftInput(requireActivity())
                when (view.id) {
                    R.id.av_head -> {
                        if (item is UserBasicVo) {
                            Router.builder(RoutePath.USER_SPACE_ACTIVITY)
                                .withInt("page", UserConst.From.UNKNOWN)
                                .withLong("userId", item.userId)
                                .withAny("userBasic", item)
                                .navigation(requireActivity())
                        }

                    }

                    R.id.user_cl -> {
                        if (item is UserBasicVo) {
                            if (item.userId != UserManager.getUserId()) {
                                Router.builder(RoutePath.MESSAGE_ACTIVITY)
                                    .withInt("page", UserConst.From.UNKNOWN)
                                    .withString("nickname", item.nickname)
                                    .withString("avatar", item.avatar)
                                    .withLong("userId", item.userId)
                                    .navigation()
                            }
                        }
                    }

                    R.id.room_cl -> {
                        if (item is DisplayUserBean) {
                            if (item.base.userId == UserManager.getUserId()) {
                                mViewModel.request({ liveService.getPartyRoom() }, {
                                    if (it.roomId?.isNotEmpty() == true) {
                                        val liveType = UserManager.getUser().live?.liveType
//                                    StartLiveProxy.builder(this)
//                                        .recoverLive(mViewModel, liveType, it.roomId)
                                    } else {
                                        //开启派对
                                        Router.builder(RoutePath.SET_PARTY_COVER_ACTIVITY)
                                            .navigation(requireActivity())
                                    }
                                })
                            } else {
                                GlobalPopupManager.showCompletePopup(requireContext(), isOk = {
                                    LiveUtils.invokeSpectator(requireContext(), item)
                                })
                            }
                        }

                    }
                }
            }


        }
    }

    override fun onSearchUserCallback(list: List<UserBasicVo>) {
        if (list.isNotEmpty()) {
            mBinding.recycler.visibility = View.VISIBLE
            mBinding.ivLogo.visibility = View.GONE
            mAdapter.setList(list)
        } else {
            mAdapter.data.clear()
            mAdapter.notifyDataSetChanged()
            mBinding.recycler.visibility = View.GONE
            mBinding.ivLogo.visibility = View.VISIBLE
        }
    }

    override fun onSearchRoomCallback(list: List<DisplayUserBean>) {
        if (list.isNotEmpty()) {
            mBinding.recycler.visibility = View.VISIBLE
            mBinding.ivLogo.visibility = View.GONE
            mAdapter.setList(list)
        } else {
            mAdapter.data.clear()
            mAdapter.notifyDataSetChanged()
            mBinding.recycler.visibility = View.GONE
            mBinding.ivLogo.visibility = View.VISIBLE
        }
    }

}